/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClassFrame;

import Object.Question;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 *
 * @author Black Diamond
 */
public class XMLParser {

    public Question[] getQuestionList() {
        Question[] qList = null;
        InputStream in = this.getClass().getResourceAsStream("/data/question.xml");
        InputStreamReader is = null;
        try {
            is = new InputStreamReader(in, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        KXmlParser parser = new KXmlParser();
        int i = 0;
        try {
            parser.setInput(is);
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "dhbc");
            qList = new Question[Integer.parseInt(parser.getAttributeValue(0))];
            String path = null, answer = null, hint = null;
            while (parser.next() != XmlPullParser.END_DOCUMENT) {
                if (parser.getEventType() == XmlPullParser.START_TAG) {
                    String tag = parser.getName();
                    if ("question".equals(tag)) {
                        path = null;
                        answer = null;
                        hint = null;
                    } else if ("path".equals(tag)) {
                        path = parser.nextText();
                    } else if ("answer".equals(tag)) {
                        answer = parser.nextText();
//                        System.out.println("Answer:" + answer);
                        System.out.println("Answer-unsign:" + Utils.signToUnsign(answer));
                    } else if ("hint".equals(tag)) {
                        hint = parser.nextText();
                    }
                } else if (parser.getEventType() == XmlPullParser.END_TAG) {
                    String tag = parser.getName();
                    if ("question".equals(tag)) {
                        qList[i] = new Question(path, answer, hint);
//                        System.out.println(i + ".Path=" + qList[i].getPath());
                        i++;
                    }
                }
            }
        } catch (XmlPullParserException ex) {
        } catch (IOException ex) {
        }
        return qList;
    }
}